package com.koron.order.zsjRepair.service;


import com.koron.bean.base.Response;
import com.koron.common.core.util.CodeTools;
import com.koron.order.defect.bean.vo.DefectVO;
import com.koron.order.zsjRepair.bean.PicFile;
import com.koron.order.zsjRepair.bean.RecordFile;
import com.koron.order.zsjRepair.bean.RemoteRecord;
import com.koron.order.zsjRepair.bean.StepFile;
import com.koron.order.zsjRepair.mapper.ZsjMapper;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ZsjRepairServiceImpl implements ZsjRepairService{

	@Override
	@TaskAnnotation("queryListByWorkerId")
	public List<DefectVO> queryListByWorkerId(SessionFactory factory, String worker){
		//首先获取与用户相关的工单
		Assert.isTrue(!StringUtils.isEmpty(worker),"账号信息不能为空");
		ZsjMapper mapper = factory.getMapper(ZsjMapper.class);
		List<String> orderIds = new ArrayList<>();
		orderIds.addAll(mapper.queryOrderByWorker(worker , 0));
		orderIds.addAll(mapper.queryOrderByWorker(worker , 1));
		List<DefectVO> orders = new ArrayList<>();
		// 没有待执行工单,返回null
		if(orderIds.size() != 0)
			//根据工单id查询工单信息,并设置工作流状态限制
			orders = mapper.queryOrderByIdsWithStatuInExcute(orderIds);
		return orders;
	}

	@Override
	@TaskAnnotation("queryRepairByName")
	public List<DefectVO> queryRepairByName(SessionFactory factory, String worker, String title){
		//首先获取与用户相关的工单
		Assert.isTrue(!StringUtils.isEmpty(worker),"账号信息不能为空");
		ZsjMapper mapper = factory.getMapper(ZsjMapper.class);
		List<String> orderIds = new ArrayList<>();
		orderIds.addAll(mapper.queryOrderByWorker(worker , 0));
		orderIds.addAll(mapper.queryOrderByWorker(worker , 1));
		List<DefectVO> orders = new ArrayList<>();
		// 没有待执行工单,返回null
		if(orderIds.size() != 0)
			//根据工单id和名称查询工单信息,并设置工作流状态限制
			orders = mapper.queryRepairOrderByName(orderIds, title);
		return orders;
	}

	@Override
	@TaskAnnotation("getDeviceTypeByOrderId")
	public 	String getDeviceTypeByOrderId(SessionFactory factory, String id){
		ZsjMapper mapper = factory.getMapper(ZsjMapper.class);
		Assert.isTrue(StringUtils.isNotBlank(id),"工单id不能为空");
		return mapper.getDeviceTypeByOrderId(id);
	}

	@Override
	@TaskAnnotation("saveOrderInfoWithRemote")
	public Response saveOrderInfoWithRemote(SessionFactory factory, RemoteRecord record){
		ZsjMapper mapper = factory.getMapper(ZsjMapper.class);

		//先要删掉原有的文件
		//先删子表
		mapper.deleteStepFileWithRemoteById(record.getCardId(),record.getProjectId());

		//再删主表
//		mapper.deleteFileWithRemoteById(record.getCardId(),record.getProjectId());

		// 根据数据库里面是否有工单和工卡组合唯一的数据，有则判断为此次数据来自于再次检修，不需要在提供id，也不需要再存一遍
		// 如果要区分不同次的检修结果，此处需要再次创建id保存，且用户也要做区分，暂定不区分
		if (mapper.selectByOrderAndCard(record.getProjectId(), record.getCardId()) < 1) {
			record.setId(StringUtils.isBlank(record.getId()) ? CodeTools.getCode32() : record.getId());
			Assert.isTrue(mapper.saveRemoteRecord(record) > 0, "保存主表失败");
		}

		// 然后再存步骤附件表主表
		// 此处再次检修不会包含原有的图片，可以直接设置eamId和保存
		// 如果要区分不同次的检修，这里可以加一个字段区分不同次的检修结果，暂定为不区分
		for(StepFile stepFile : record.getStepList()){
			// 如果没有图，则在步骤层次生成主键id，目的是保存步骤内容，为了后面可能设置的工卡删除，记录不变，如果要区别于图Id，可以在生成时加上Step前缀
			if(CollectionUtils.isEmpty(stepFile.getPicFiles())){
				stepFile.setEamId(CodeTools.getCode32());
				continue;
			}
			// 正常情况下，主键和在图层次，因为一个步骤能有多个图
			for(PicFile picFile : stepFile.getPicFiles()){
				picFile.setEamId(CodeTools.getCode32());
			}
		}
		// 目前是连表查询，如果工卡被删除，其记录也就查不到了，但记录保存了足够信息，可以改为单表查询，但这样就会导致工卡改变，记录不会变
		// 也可以改为工卡表不空，连表，为空则单表查，暂定为连表
		if(!CollectionUtils.isEmpty(record.getStepList()))
			mapper.saveRemoteStepFiles(record.getProjectId(),record.getStepList(),"StepFile");

		// 要区分不同次检修的话，需要加字段加以区分，暂定为不区分，每次修改结果，都全部删除再新增
		if(!CollectionUtils.isEmpty(record.getRecordFiles())) {
			for(RecordFile recordFile:record.getRecordFiles()){
				recordFile.setEamId(CodeTools.getCode32());
			}
			mapper.saveRemoteRecordFile(record.getProjectId(), record.getCardId(), record.getRecordFiles(), "RecordFile");
		}
		return Response.ok("保存成功");
	}

	@Override
	@TaskAnnotation("getOrderInfoWithRemote")
	public List<RemoteRecord> getOrderInfoWithRemote(SessionFactory factory, String orderId){
		ZsjMapper mapper = factory.getMapper(ZsjMapper.class);
		Assert.isTrue(StringUtils.isNotBlank(orderId),"工单id不能为空");
		List<RemoteRecord> records = mapper.SelectCardIdWithRecord(orderId);

		for(RemoteRecord record : records){
			// 获取步骤执行信息
			List<StepFile> stepFiles = mapper.SelectStep(record.getCardId(),record.getProjectId(),"StepFile");
			// 如果步骤里面没有文件id，则设置文件列表为空
			stepFiles.stream().forEach(stepFile -> {
				if(CollectionUtils.isEmpty(stepFile.getPicFiles().stream().map(PicFile::getFileId).filter(s -> StringUtils.isNotEmpty(s) ).collect(Collectors.toList())))
					stepFile.setPicFiles(null);
			});
			record.setStepList(stepFiles);
			// 获取视频记录
			List<RecordFile> recordFiles = mapper.SelectRecordFile(record.getCardId(),record.getProjectId(),"RecordFile");
			record.setRecordFiles(recordFiles);
		}

		return records;
	}
	@Override
	@TaskAnnotation("deleteStepFileWithRemote")
	public Integer deleteStepFileWithRemote(SessionFactory factory, String fileId){
		ZsjMapper mapper = factory.getMapper(ZsjMapper.class);
		Assert.isTrue(StringUtils.isNotBlank(fileId),"文件id不能为空");
		return mapper.deleteStepFileWithRemote(fileId);
	}
}
